package com.weavernorth.retailExperienceAward.service;

import com.weavernorth.sysap.util.SyncUtil;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
/**
 * @Author: bruseewang
 * @Package: com.weavernorth.retailExperienceAward.service
 * @date: 2020/7/15 10:31
 * @description: 将月度店铺业改装（闭店）修信息单中的数据插入到店铺统计表中
 */
public class SendDataToZddptjService {
    public Logger log;
    public Logger getLog() {
        return log;
    }
    public void setLog(Logger log) {
        this.log = log;
    }
    public Integer cycle;
    public Integer getcycle() {
        return cycle;
    }
    public void setcycle(Integer cycle) {
        this.cycle = cycle;
    }
    public void execute(){
        log.info("插入数据到店铺统计表接口执行开始------------");
        RecordSetTrans rsTrans;
        SyncUtil syncUtil = new SyncUtil();
        RecordSet rs_update=new RecordSet();
        String strFormid = syncUtil.getModeIdBytableName("uf_zddpfstj");
//        String strInsert="INSERT INTO uf_zddpfstj (fgs,dp,zdf,khzq,khrq,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime)VALUES(?,?,?,?,?,?,?,?,?)";
        String strKhZq=dateUtils.previousMonth(0);
        if (cycle!=null){
            strKhZq=dateUtils.previousMonth(cycle);
        }
        String strSql="SELECT * FROM uf_yddpzxygxxd WHERE sfyqs=0 or sfyqs is null and khzq='"+strKhZq+"'";
        String nowDate=dateUtils.getYearMonthDay();
        log.info("当前日期："+nowDate);
        String nowTime=dateUtils.getHourMs();
        log.info("当前时间："+nowTime);
        RecordSet rs=new RecordSet();
        rs.executeQuery(strSql);
        while (rs.next()){
            String strDp= Util.null2String(rs.getString("dp"));
            log.info("店铺="+strDp);
            String strFgs=Util.null2String(rs.getString("fgs"));
            log.info("分公司="+strFgs);
            String strZq=Util.null2String(rs.getString("khzq"));
            log.info("考核周期="+strZq);
            int year= Integer.parseInt(strZq.substring(0,4));
//            log.info("年="+year);
            int month= Integer.parseInt(strZq.substring(4,6));
//            log.info("月="+month);
            String strRq= dateUtils.getLastDayOfMonth(year,month);
//            log.info("日期="+strRq);
            Boolean resultExe=isExists(strZq,strDp,strFgs,log);
            if (!resultExe){
                rsTrans=new RecordSetTrans();
                try{
                    rsTrans.setAutoCommit(false);
                    String strInsert="INSERT INTO uf_zddpfstj (fgs,dp,dpzt,zdf,khzq,khrq,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) "+
                            "VALUES('"+strFgs+"','"+strDp+"','2','90','"+strZq+"','"+strRq+"','"+strFormid+"',1,0,'"+nowDate+"','"+nowTime+"')";
                    log.info("插入数据sql:"+strInsert);
                    rsTrans.executeUpdate(strInsert);
//                    rsTrans.executeUpdate(strInsert,strFgs,strDp,90,strZq,strRq,strFormid,"1","0",nowDate,nowTime);
                    rsTrans.commit();
                    log.info("插入到店铺分数统计表成功！");
                    String strUpdate="update uf_yddpzxygxxd set sfyqs=1 where fgs="+strFgs+" and dp="+strDp+" and khzq="+strZq+"";
                    rs_update.executeQuery(strUpdate);
                }catch (Exception ex){
                    log.info("插入到店铺分数统计表出现异常,异常信息："+ex.toString());
                    ex.printStackTrace();
                    rsTrans.rollback();
                    break;
                }
            }
        }
        log.info("接口执行结束------------");


    }
    /***
     * 判断是否已经存在数据
     * @param strZq
     * @param strDp
     * @param strFgs
     * @return
     */
    public static Boolean isExists(String strZq,String strDp,String strFgs,Logger log) {
        RecordSet rs = new RecordSet();
        try {
            String strSql = "SELECT 1 FROM uf_zddpfstj WHERE fgs='" + strFgs + "' AND dp=" + strDp + " AND khzq='" + strZq + "'";
            rs.executeQuery(strSql);
            int rows=rs.getCounts();
            if (rows>0){
                return true;
            }else {
                return false;
            }
        }catch (Exception ex){
            ex.printStackTrace();
            log.info("查询数据出现异常："+ex.toString());
            return  false;
        }

    }
}
